package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000116662056";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCWq6DC3W4gMhW8jl6moql0EWBidftRoLr99Opwwu+s0BeXcXI8jrob4bigqK2+Sd6zXe4R/DNCJzDHLd7lILLh09cHASlgTyUNLIcrMfpYY8QQ9lYWck1foHLcfdoD7cvQUo6gG71MjgQbo1H596N5umnTwEMLztP4o4wT86cWA8xddI3y9ICG+rRb3C9/BbtYXMzJUMYs09FNYq5aygaSGT0MA+yePsIG/pnARmdjeyslEBoQgcTwN2hhwTIueew7xLYrzRyYlHHFSFeWAYYla4RFu+Gc4EnYvPloECIohiS0JGzz7ocGSj97zWTSTuIbk7OeIYwkEvW0FarI4ch5AgMBAAECggEAG47qyZmcsfDXPvS/ZTUtT1h6S6gOvsBqU1b/pn5RPkeVeRK79U2Zxs3DakdP+BUk0aPljo6VhY0Lg/Lk6P3RGjMYCmTw/3eabE1vr7vlcVNWIlAcMODZPBtpxuCSBtPwynq/MZPBTQZQxAlcTh84XGR3RM7OtJMw/DJVxbR0JDdnyn24v7C/Isjm3FwOXAr3GpDq4tm0KfddK+I79sRTJVbegE93JV0P6nNQrLRIktaBt0EbwbOCDBcImoiuCp0rz0dvTWjT+LCiYOac1Aeua8ktsXO+qUWuxCe1IDmBITQmk0rT/g2uMx99/XMi2Xw3+srBGKlVzLoxUsUGGXWC8QKBgQDFDWbdvwEQxDi7auIrqq+4fp0im/kbNYEteLr+BqUqLwqXz4kTWPNbdaA7FJkBkJczVBzsfZHlJUwLalRlCq7Iqjnz0xsABuEQP0GTrwpljUHNGB0o1Pfyf1wfKPtXRi0VVC5kw4Jso+Um2IGIrRjVQdothsM0dKNMzs8FKZww/QKBgQDDvjh1zjgvzuMq/svIwV1P2cUD/GxtD40W9UdwPOboWo6YcJyagfa81kfxXid8m8oCEooVJW/OCFw3n471mqNB4pWo74MpdRdpxu3OKTPFItFVDZjpEUf0C4YdEn8u0TxBQQy029YW/izymm5CQjutqhr/Qk6iuwhr5v+Yxs+cLQKBgFOyCYsN4an5bg9MqpvZd6FpRdy5WukPpLpnmSTkNarhKCKq55HsKhEiuNffO83KWC5Y/6ADBcDKQq5mj0oLa4mXo93IQKOWSGnaNERCCKxxfw1ZvKjnLBk3ty0U/wxmpid1Be+OiwsR4fbJ5triirWprr2/i5HwKJwrCwsqTUkRAoGAFWWpzlbolXFMK9sGc7+Pb7cibjkdsR5OEgBl+sLtiuC7Tbahp9IlIqdZ3eyo7ForTuav6XfaLO+FBR+QBREOX5lhOsJT2ChQkuOIA01Ft3sC3wVJfdDByM+prWvkMz30FD4EIZEWahUG3M4810leNLGvmc+pwOgpF4J81jqFX1ECgYBnqjjTEKK03PVlYkNPJtW/0/4AveCILoeR7vC7v+rneNtTHJ3bNxM7mLmsb41zPeFTe+a8U6GVER7Uv2MFcNjI8we7d7TAywAVluchmd5pPp2l+5YGZAhI1NdaI6GeOjo2fBNYi+MkV85ouHv71DDrSnYXXdvlf3UVYeKJogwGTQ==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key =  "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhsBkRLmzG4Csr+EjywXppjESaVPnv/J/ZgPlcEDF5FD+9rrNwQE2+wtTrFv5UYKf3H85OCbaolVClW395qS4G9TncckNLVhC4w1tzwrhtlYdVx0BVJyJRcSf2zU/rghcQfkBx0sFCdTqUr/ZI2dANLWKa/2n0iaMyQyU+ykbJAVYl5iyksJ1fI39nHWZc0eQf1LMpE7lhmCikAL0lVNBRZImiCYgTfnBfpkVAwU95HeS6sadOachaj7m8v230BBcEKVIhACEnIRAjOYXuNdAONrFiW22B53I9VriAZYgDYTmH1vLJE2BNWdYi039N9/+xAnxwUyO9Bpq0/8b0K3XYwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url ="http://9al7g94sem.52http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url ="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                +"\"timeout_express\":\"1m\","

                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
